<?php 
/**
 * 厂家入库欠款 object_type 220 
 * @copyright   Copyright (c) 2006 - 2010 DZH 友拓软件友拓通
 * @category   	账目信息
 * @package  	Model
 * @author    	李恺弘
 * @version 	2.1,2012-07-22
 */

class FactoryInstockPublicModel extends AbsFundsPublicModel {
	//款项类型
	public $object_type	=	220;//厂家入库欠款
	//对象类型
	public $comp_type	=	2;	
   
	/**
	 * 销售单欠款
	 *
	 * @param int $id
	 * @return array
	 */
	public function _fund($id){  
		$id	=	is_array($id)?$id['id']:$id;
		if($id<=0){
			return ;
		}
		//上次生成的款项ID
		$old_id_array	=	$this->getBeforPaidIdArray($id);
 		  
		$model	= M('instock_detail');
	 	$info	= $model
	 			->field( 	''.C('main_basic_id').' as basic_id,a.currency_id as currency_id,date(b.real_arrive_date) as paid_date,
							a.factory_id as comp_id,'.$this->comp_type.' as comp_type,a.instock_id as object_id,instock_no as reserve_id,
							sum(quantity*capability*dozen*price) as money,c.paid_id as id,
							'.$this->object_type.' as object_type,c.have_paid,c.is_close
							')
						->join('as a 
								left join instock as b on a.instock_id=b.id and b.id='.$id.' 
								left join factory_paid_detail as c 
									on c.object_id=a.instock_id 
									and a.factory_id=c.comp_id  
									and object_type='.$this->object_type.' 
									and c.currency_id=a.currency_id   
									')
						->where('a.instock_id='.$id)
						->group('a.factory_id,a.currency_id')
						->select();     
		//插入(修改)多个厂家欠款
		foreach ((array)$info as $key=>$row) { 
		    $comp_name			=	SOnly('factory',$row['comp_id']);
			$row['comp_name']	=	$comp_name['factory_name']; 
			if (($row['have_paid']==0 || empty($row['have_paid'])) && $row['is_close']==0 ) {//未指定支付的可以修改款项 
				$vo[]	=	$this->_saveFunds($row);
			} 
		} 
		//删除款项id差集
		$this->array_diff_id_key($old_id_array,$info);  
		//预付款 
		return $vo;
	}
	
	/**
	 * 删除
	 *
	 * @param int $id 入库单号
	 * @return array
	 */
	public function deleteOp($id){ 
		$id	=	is_array($id)?$id['id']:$id;
		//入库ID
		if($id>0) {
			//获取上次款项表中的ID
			$paid_array	=	$this->getBeforPaidIdArray($id); 
			if(count($paid_array)>0) { 
				//款项表ID 
				$vo	=	$this->_deleteFunds($paid_array); 
				return $vo; 
			}
		} 
	}
	 
	/**
	 * 验证是否可修改
	 *
	 * @param array $info
	 * @return array
	 */
	public function checkEdit($info){ 
		return true;
		return $this->checkHaveCloseOut($info); 
	}
	 
	/**
	 * 验证是否可删除
	 *
	 * @param array $info
	 * @return array
	 */
	public function checkDelete($info){  
		return $this->checkHaveCloseOut($info);
	}
	 
	/**
	 * 验证多个厂家是否被对账或总平
	 *
	 * @param array $info
	 * @return array
	 */
	private function checkHaveCloseOut($info){
		$error['state']	=	1;
		//装柜单ID 
		$id	=	is_array($info)?$info['id']:$info; 
		//验证是否被对账或者总平
		if ($id>0) {
			$model		= M($this->getCompPaidDetail($this->comp_type));
		 	$paid_info	= $model 
							->where('object_id='.$id.' and object_type='.$this->object_type.' ')
							->select();  
			//插入(修改)多个厂家欠款
			foreach ((array)$paid_info as $key=>$row) {  
				//验证是否被总平或者对账
				$error				=	$this->checkAccountDate($row,'delete');
				if ($error['state']==-1) {
					break;
				}
				//验证是否被指定支付
				$error	=	parent::checkDelete($row);
				if ($error['state']==-1) {
					break;
				}
			} 
		}else{
			$error['state']			=	-1; 
			$error['error_msg'][]	=	L('error_funds_values_error');//款项验证,参数错误,请重新操作
		}   
		return $error; 
	}
	 
	/**
	 * 插入前的业务规则验证
	 *
	 * @param array $info
	 * @return array
	 */
	public function checkInsert($info){    
		return	$this->checkFactoryDate($info);  
	}
	 
	/**
	 * 修改插入数据前的业务规则验证
	 *
	 * @param array $info
	 * @return array
	 */
	public function checkUpdate($info){ 
		return	$this->checkFactoryDate($info);  
	} 
	
	/**
	 * 验证某日期以前的款项是否可以被插入
	 *
	 * @param array $info
	 * @return array
	 */
	public function checkFactoryDate($info){
		$error['state']	=	1;   
		$info['comp_type']	=	$this->comp_type; 
		if ($this->validCheckAccountDate($info['real_arrive_date'],$info)===false){
			$error['state']	=	-1;
			$error['error_msg'][]	=	$info['real_arrive_date'].L('error_input_paid_date_is_max');//已指定操作,不可删除
		} 
		return $error;
	}
	 
	/**
	 * 过滤掉不可修改入库的厂家
	 *
	 * @param int $id
	 * @return array
	 */
	public function EditPassInfo($id){ 
		$model	=	M($this->getCompPaidDetail($this->comp_type));
		$list	=	$model->field('comp_id,currency_id')
		->where('( have_paid>0 || is_close>0 ) and object_type='.$this->object_type.' and object_id='.$id)
		->select(); 
		return $list;
	}
	
	
}
?>